package com.vibeplayer.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.vibeplayer.app.MainActivity;
import com.vibeplayer.app.R;
import com.vibeplayer.app.activity.FileExplorerActivity;
import com.vibeplayer.app.adapter.SongAdapter;
import com.vibeplayer.app.model.Song;
import com.vibeplayer.app.service.MusicPlayerService;
import com.vibeplayer.app.util.MediaScanner;

import java.util.ArrayList;
import java.util.List;

public class SongsFragment extends Fragment implements SongAdapter.SongClickListener {

    private static final int REQUEST_IMPORT_MUSIC = 1001;

    private RecyclerView recyclerView;
    private SongAdapter adapter;
    private List<Song> songs = new ArrayList<>();
    private TextView txtNoSongs;
    private MediaScanner mediaScanner;
    private FloatingActionButton fabImportMusic;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_songs, container, false);
        
        recyclerView = view.findViewById(R.id.recyclerViewSongs);
        txtNoSongs = view.findViewById(R.id.txtNoSongs);
        fabImportMusic = view.findViewById(R.id.fabImportMusic);
        
        // 初始化媒体扫描器
        mediaScanner = new MediaScanner(requireContext());
        
        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new SongAdapter(getContext(), songs, this);
        recyclerView.setAdapter(adapter);
        
        // 设置导入按钮点击事件
        if (fabImportMusic != null) {
            fabImportMusic.setVisibility(View.VISIBLE);
            fabImportMusic.setOnClickListener(v -> openFileExplorer());
        }
        
        // 加载歌曲
        loadSongs();
        
        return view;
    }
    
    @Override
    public void onResume() {
        super.onResume();
        // 刷新歌曲列表
        loadSongs();
    }
    
    private void loadSongs() {
        songs.clear();
        songs.addAll(mediaScanner.getAllSongs());
        
        if (songs.isEmpty()) {
            txtNoSongs.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            txtNoSongs.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged();
        }
    }
    
    @Override
    public void onSongClick(int position) {
        // 通知MainActivity播放选中的歌曲
        if (getActivity() instanceof MainActivity) {
            MainActivity activity = (MainActivity) getActivity();
            if (activity.isServiceBound()) {
                MusicPlayerService musicService = activity.getMusicService();
                if (musicService != null) {
                    musicService.setSongs(songs);
                    activity.playSong(position);
                }
            }
        }
    }
    
    /**
     * 打开文件浏览器
     */
    private void openFileExplorer() {
        Intent intent = new Intent(getActivity(), FileExplorerActivity.class);
        startActivityForResult(intent, REQUEST_IMPORT_MUSIC);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMPORT_MUSIC && resultCode == getActivity().RESULT_OK) {
            // 导入完成后刷新歌曲列表
            loadSongs();
        }
    }
} 